﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class HocVien_frmBaiThi : System.Web.UI.Page
{
    clsQtDeThi qtdt = new clsQtDeThi();
    clsBaiThi bt = new clsBaiThi();
    clsChiTietBT ctbt = new clsChiTietBT();
    clsKetQuaBaithi kqbt = new clsKetQuaBaithi();
    public double seconds;
    protected void Page_Load(object sender, EventArgs e)
    {

                
        if (!IsPostBack)
        {
            
            lblMathisinh.Text = Session["TenDangNhap"].ToString();
            lblNgayGio.Text = DateTime.Now.ToShortDateString();
            lblMaDT.Text = Request.QueryString["madethi"];
            lblmabt.Text = lblMathisinh.Text + lblMaDT.Text;
            seconds = 185;
            //seconds = qtdt.thoigian(lblMaDT.Text);
            getdata();
            //load so cau hoi

            foreach (DataListItem item1 in DataList1.Items)
            {
                foreach (DataListItem item2 in DataList3.Items)
                {
                    if (((Label)item2.FindControl("lblCauHoi")).Text == "Number")
                    {
                        string gt = ((Label)item1.FindControl("lblstt")).Text;
                        ((Label)item2.FindControl("lblCauHoi")).Text = "Câu " + ((Label)item1.FindControl("lblstt")).Text;
                         
                        ((Label)item2.FindControl("lblCauHoi")).Attributes.Add("onclick", "run("+gt+");");
                        break;
                    }
                    //string gt = ((Label)item2.FindControl("lblCauHoi")).Text;
                    //((Label)item2.FindControl("lblCauHoi")).Attributes.Add("onclick", "run(" + gt + ");");
                    
                }
            }

        }
    }
    public void getdata()
    {
        List<clsBaiThi> l = new List<clsBaiThi>();
        List<int> listnumber = new List<int>();
        l = bt.getdata(lblMaDT.Text);
        DataList1.DataSource = l;
        DataList1.DataBind();

        for (int i = 1; i <= l.Count; i++)
        {
            listnumber.Add(i);
        }
        DataList3.DataSource = listnumber;
        DataList3.DataBind();

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string mach = string.Empty;
        string traloi = string.Empty;
        string mabt=lblmabt.Text;

        string madt = lblMaDT.Text;
        string ngaythi = lblNgayGio.Text;
        string tenbt = lblTenBaiThi.Text;
        string masv = lblMathisinh.Text;

        kqbt.ThemBaiThi(madt, mabt, ngaythi, tenbt, masv);
        foreach (DataListItem item in DataList1.Items)
        {

            string lblmach = ((Label)item.FindControl("lblMaCH")).Text;
            //mach = lblmach.Text;
            mach = lblmach;
            RadioButton rd2 = item.FindControl("RadioButton5") as RadioButton;
            //rd2.Checked = false;
            RadioButton rd3 = item.FindControl("RadioButton6") as RadioButton;
            //rd3.Checked = false;
            RadioButton rd4 = item.FindControl("RadioButton7") as RadioButton;
            //rd4.Checked = false;
            RadioButton rd5 = item.FindControl("RadioButton8") as RadioButton;

            List<RadioButton> rd = new List<RadioButton>();
            rd.Add(rd2);
            rd.Add(rd3);
            rd.Add(rd4);
            rd.Add(rd5);
            foreach (var rdb in rd)
            {
                if (rdb.Checked == true)
                {
                    traloi = rdb.Text;
                    break;
                }
                else
                {
                    traloi = "null";
                }
            }

            ctbt.CapnhatCTBT(mabt, mach, traloi);

        }
        kqbt.KetQua(mabt, madt);

        string url = "";
        url = "frmKetQuaThi.aspx?mabaithi=" + lblmabt.Text;
        Response.Redirect(url);

        //Response.Redirect("frmKetQuaThi.aspx");
       
    }

    public void CheckboxEvent()
    {
        foreach (DataListItem item2 in DataList3.Items)
        {
            string lblitem2 = ((Label)item2.FindControl("lblCauHoi")).Text;
                           //((Label)item2.FindControl("lblstt")).Text;
            
                foreach (DataListItem item in DataList1.Items)
                {
                    string lblitem = ((Label)item.FindControl("lblstt")).Text;
                    if (string.Compare("Câu "+lblitem,lblitem2)==0)
                    {
                        RadioButton rd2 = item.FindControl("RadioButton5") as RadioButton;
                        //rd2.Checked = false;
                        RadioButton rd3 = item.FindControl("RadioButton6") as RadioButton;
                        //rd3.Checked = false;
                        RadioButton rd4 = item.FindControl("RadioButton7") as RadioButton;
                        //rd4.Checked = false;
                        RadioButton rd5 = item.FindControl("RadioButton8") as RadioButton;

                        List<RadioButton> rd = new List<RadioButton>();
                        rd.Add(rd2);
                        rd.Add(rd3);
                        rd.Add(rd4);
                        rd.Add(rd5);

                        foreach (var rdb in rd)
                        {
                            if (rdb.Checked == true)
                            {
                                //((Label)item2.FindControl("lblCauHoi")).Checked = true;
                                ((Label)item2.FindControl("lblCauHoi")).BackColor = System.Drawing.Color.Red;
                                break;
                            }

                        }
                        
                    }

                   // break;
                }
            
        }

 
    }
    protected void RadioButton5_CheckedChanged(object sender, EventArgs e)
    {
        CheckboxEvent();
    }
    protected void RadioButton6_CheckedChanged(object sender, EventArgs e)
    {
        CheckboxEvent();
    }
    protected void RadioButton7_CheckedChanged(object sender, EventArgs e)
    {
        CheckboxEvent();
    }
    protected void RadioButton8_CheckedChanged(object sender, EventArgs e)
    {
        CheckboxEvent();
    }
}
